package com.atguigu.spzx.user.controller;

import com.atguigu.spzx.model.entity.order.UserAddress;
import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.user.service.UserAddressService;
import com.atguigu.spzx.util.AuthContextUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author LT
 * @create 2024-03-29-20:39
 */
@SuppressWarnings({"unchecked","userAddress"})
@RequestMapping("/api/user/userAddress")
@RestController
public class UserAddressController {

    @Autowired
    private UserAddressService userAddressService;

    @GetMapping("/auth/findUserAddressList")
    public Result<List<UserAddress>>findUserAddressList (){
        List<UserAddress> list = userAddressService.findUserAddressList();
        return Result.ok(list);
    }

    @GetMapping("/getUserAddress/{id}")
    public Result<UserAddress> getUserAddress (@PathVariable("id")Long id){
        UserAddress userAddress = userAddressService.getUserAddress(id);
        return Result.ok(userAddress);
    }

    @PutMapping("/auth/updateById")
    public Result updateById (@RequestBody UserAddress userAddress){
        if (userAddress.getIsDefault().intValue() ==1){
            userAddressService.update(Wrappers.lambdaUpdate(UserAddress.class)
                    .set(UserAddress::getIsDefault , 0)
                    .eq(UserAddress::getId,userAddress.getId())
                    .eq(UserAddress::getIsDefault,1));
        }
        userAddressService.updateById(userAddress);
        return Result.ok();
    }

    @PostMapping("/auth/save")
    public Result save (@RequestBody UserAddress userAddress){
         userAddressService.saveUserAddress(userAddress);
        return Result.ok();
    }

    @DeleteMapping("/auth/removeById/{id}")
    public Result removeById(@PathVariable("id")Long id){
       //userAddressService.removeById(id);
        userAddressService.update(Wrappers.lambdaUpdate(UserAddress.class)
                .set(UserAddress::getIsDeleted,1)
                .eq(UserAddress::getIsDeleted,0)
                .eq(UserAddress::getId,id));
        return Result.ok();
    }





}
